<?php


//This function doesn't take any parameters and returns all the pdf files in the database
Function viewAll()
{
    // Open onnection with the database
     $conn = @mysql_connect("localhost" , "root", "") or die(mysql_error());
     mysql_select_db("db_name") or die (mysql_error());

  //execute query 
$query = 'SELECT `file_name`, `file_path`, `size` , `type` , `date`  FROM `pdfs`';
$result = mysql_query($query) or die (my_sql_error());

//Check that the pdf table is not empty

    if(!mysql_num_rows($result))  {
        echo '<p>There are no files in the database</p>';
    }
    else {
        // Print the top of a table
        echo '<table width="100%">
                <tr>
                    <td><b>Name</b></td>
                    <td><b>Type</b></td>
                    <td><b>Size (bytes)</b></td>
                    <td><b>Created</b></td>
                    <td><b>Download</b></td>
                    <td><b>View</b></td>
                </tr>';
        // Print each file
        while($row = mysql_fetch_assoc($result)) {
            echo "
                <tr>
                    <td>{$row['file_name']}</td>
                    <td>{$row['type']}</td>
                    <td>{$row['size']}</td>
                    <td>{$row['date']}</td>
                    <td><a href='download.php?file_name={$row['file_name']}'>Download</a></td>
                    <td><a target='_blank' href='viewfile.php?file_name={$row['file_name']}'>View</a></td>           
                    </tr>"; 
        }
        // Close table
        echo '</table>';
        //Close connection
        mysql_close($conn);
    }
}
// this function takes a file name and returns the file's information 
Function getFile ($fileName)
{
    $conn = @mysql_connect("localhost" , "root", "") or die(mysql_error());
                    mysql_select_db("db_name") or die(mysql_error());
                    
       //execute query and get the file information
        $query = "
            SELECT  `file_name`,`type`, `size`, `date`
            FROM `pdfs`
            WHERE `file_name` = '$fileName';";
             $result = mysql_query($query);
             
      echo '<table width="100%">
                <tr>
                    <td><b>Name</b></td>
                    <td><b>Type</b></td>
                    <td><b>Size (bytes)</b></td>
                    <td><b>Created</b></td>
                    <td><b>Download</b></td>
                    <td><b>View</b></td>
                </tr>';
        // Print the info
        while($row = mysql_fetch_assoc($result)) {
            echo "
                <tr>
                    <td>{$row['file_name']}</td>
                    <td>{$row['type']}</td>
                    <td>{$row['size']}</td>
                    <td>{$row['date']}</td>
                    <td><a href='download.php?file_name={$row['file_name']}'>Download</a></td>
                    <td><a target='_blank' href='viewfile.php?file_name={$row['file_name']}'>View</a></td>           
                    </tr>"; 
        }
        // Close table
        echo '</table>'; 
        //Close connection
        mysql_close($conn);
}


//This function searches for pdf files that are written by the author which is passed as a parameter
Function searchPDF_author($authorneeded)
{
    // Open connection with the database
       $conn = @mysql_connect("localhost" , "root", "") or die(mysql_error());
         mysql_select_db("db_name") or die(mysql_error());
         
          //execute query 
         $result = mysql_query("SELECT * 
                 FROM pdfs p , Members m 
                 WHERE p.author_username = m.username 
                 AND p.author_username = '$authorneeded' 
                 AND m.username= '$authorneeded'; ");
       
           echo '<table width="100%">
                <tr>
                    <td><b>Name</b></td>
                    <td><b>Type</b></td>
                    <td><b>Size (bytes)</b></td>
                    <td><b>Created</b></td>
                    <td><b>Download</b></td>
                    <td><b>View</b></td>
                </tr>';
        // Print each file
        while($row = mysql_fetch_assoc($result)) {
            echo "
                <tr>
                    <td>{$row['file_name']}</td>
                    <td>{$row['type']}</td>
                    <td>{$row['size']}</td>
                    <td>{$row['date']}</td>
                    <td><a href='download.php?file_name={$row['file_name']}'>Download</a></td>
                    <td><a target='_blank' href='viewfile.php?file_name={$row['file_name']}'>View</a></td>           
                    </tr>"; 
        }
        // Close table
        echo '</table>';
             
         //Close connection
         mysql_close($conn);
                    
}
//This function searches for all pdfs written on the specified date which is passed as a parameter
Function searchPDF_date($date)
{
        // Open connection with the database

        $conn = @mysql_connect("localhost" , "root", "") or die(mysql_error());
         mysql_select_db("db_name") or die(mysql_error());
         //execute query
         $result = mysql_query("SELECT * 
                 FROM pdfs p
                 WHERE p.date = '$date';"); 
                 
          echo '<table width="100%">
                <tr>
                    <td><b>Name</b></td>
                    <td><b>Type</b></td>
                    <td><b>Size (bytes)</b></td>
                    <td><b>Created</b></td>
                    <td><b>Download</b></td>
                    <td><b>View</b></td>
                </tr>';
        // Print each file
        while($row = mysql_fetch_assoc($result)) {
            echo "
                <tr>
                    <td>{$row['file_name']}</td>
                    <td>{$row['type']}</td>
                    <td>{$row['size']}</td>
                    <td>{$row['date']}</td>
                    <td><a href='download.php?file_name={$row['file_name']}'>Download</a></td>
                    <td><a target='_blank' href='viewfile.php?file_name={$row['file_name']}'>View</a></td>           
                    </tr>"; 
        }
        // Close table
        echo '</table>';
        //close connection
        mysql_close($conn);
                    
}

                    
?>
